package com.hmkj.web.controller.my;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.user.UserBankModel;
import com.hmkj.core.po.system.Bank;
import com.hmkj.core.po.user.User;
import com.hmkj.core.po.user.UserBank;
import com.hmkj.core.service.system.BankService;
import com.hmkj.core.service.user.UserBankService;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "我的银行卡相关")
@Slf4j
@RestController
@RequestMapping(GenericController.MEMBER_URL + "/bank")
public class MyBankController extends GenericController<Integer, UserBank> {
    @Resource
    private UserBankService userBankService;
    @Resource
    private UserService userService;
    @Resource
    private BankService bankService;

    @ApiOperation(value = "个人中心-我的银行卡", notes = "我的银行卡")
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ResultEntity<List<UserBank>> list() {
        //实名认证校验
        checkRealNameStatus(getLoginUser());
        User user = getLoginUser();
        List<UserBank> userBankList = userBankService.select(new UserBank(u -> {
            u.setUserId(user.getId());
            u.setDelFlag(UserBank.DELFLAG.NORMAL.code);
        }));
        return ok(userBankList);
    }

    @ApiOperation(value = "我的银行卡", notes = "我的银行卡")
    @ResponseBody
    @RequestMapping(value = "myBankList", method = RequestMethod.POST)
    public ResultEntity<List<UserBank>> myBankList() {
        User user = getLoginUser();
        List<UserBank> userBankList = userBankService.select(new UserBank(u -> {
            u.setUserId(user.getId());
            u.setDelFlag(UserBank.DELFLAG.NORMAL.code);
        }));
        return ok(userBankList);
    }

    @ApiOperation(value = "添加", notes = "添加")
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResultEntity add(@ApiParam("银行编码") @RequestParam String bankCode,
                            @ApiParam("银行名称") @RequestParam String bankName,
                            @ApiParam("预留手机号") @RequestParam String phone,
                            @ApiParam("开户省") @RequestParam(required = false) String province,
                            @ApiParam("开户市") @RequestParam(required = false) String city,
                            @ApiParam("开户支行") @RequestParam(required = false) String branch,
                            @ApiParam("银行卡号") @RequestParam String bankNo) {
        User user = userService.selectByPrimaryKey(getLoginUser().getId());
        if (user == null) {
            throw new BussinessException("查询用户信息失败");
        }
        //实名认证校验
        checkRealNameStatus(getLoginUser());

        //基础参数校验
        UserBankModel.checkBankCard(user.getRealName(), bankCode, bankName, province, city, branch, bankNo, phone);
        userBankService.add(new UserBank(b -> {
            b.setUserId(user.getId());
            b.setRealName(user.getRealName());
            b.setBankCode(bankCode);
            b.setBankName(bankName);
            b.setBankNo(bankNo);
            b.setProvince(province);
            b.setCity(city);
            b.setMobile(phone);
            b.setBranch(branch);
            b.setStatus(UserBank.STATUS.T3.code);
            b.setCardType("01");// 银行卡类别，默认01借记卡
            b.setIdCard(user.getIdCard());
        }));
        return ok();
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public ResultEntity del(@ApiParam("主键") @RequestParam Integer bankId) {
        userBankService.del(bankId, getLoginUser().getId());
        return ok();
    }

    @ApiOperation(value = "所有银行列表", notes = "所有银行列表")
    @ResponseBody
    @RequestMapping(value = "bank", method = RequestMethod.POST)
    public ResultEntity<List<Bank>> bank() {
        return ok(bankService.select(new Bank(b -> b.setDelFlag(Bank.DELFLAG.NORMAL.code))));
    }
}
